import { Grass } from "./grass";
import { Wall } from "./wall";
import { DirectionType } from ".";
import { StaticBlack } from "./staticBlack";

enum enumKey {
  null = 0,
  Grass = 1,
  Wall = 2,
}
function getRandomEnumValue(): enumKey {
  const values = Object.values(enumKey);
  const randomIndex = Math.floor(Math.random() * values.length);
  return values[randomIndex] as enumKey;
}
//地图16*16
class MainMap {
  mapZise = [16, 16];
  #map: Array<Array<enumKey>> = [];
  #valueMap: Map<string, StaticBlack> = new Map();
  removeMapBlack(black: StaticBlack) {
    const x = Math.floor((black.targter.x || 0) / 50);
    const y = Math.floor((black.targter.y || 0) / 50);
    return this.#valueMap.delete(`${y}${x}`);
  }

  randomGenerateMap() {
    for (let index = 0; index <= this.mapZise[1]; index++) {
      const arry: Array<enumKey> = [];
      for (let indey = 0; indey < this.mapZise[0]; indey++) {
        if (
          (index < 3 && indey < 3) ||
          (index >= this.mapZise[0] - 3 && indey >= this.mapZise[1] - 3)
        )
          arry.push(enumKey.null);
        else arry.push(getRandomEnumValue());
      }
      this.#map.push(arry);
    }
    return true;
  }
  addMapBlack(black: StaticBlack) {
    const x = Math.floor((black.targter.x || 0) / 50);
    const y = Math.floor((black.targter.y || 0) / 50);
    return this.#valueMap.set(`${y}${x}`, black);
  }
  setMap(arr: Array<Array<enumKey>>) {
    this.#map = arr;
  }
  draw() {
    this.#valueMap.clear();
    this.#map.forEach((item, index) => {
      item.forEach((value, index2) => {
        switch (value) {
          case enumKey.Grass:
            new Grass({ x: index2 * 50, y: index * 50 });
            break;
          case enumKey.Wall:
            new Wall({ x: index2 * 50, y: index * 50 });
            break;
          default:
            break;
        }
      });
    });
  }
  // 获取坐标对应方向上的实体方块
  getPointVicinity({
    x,
    y,
    actionDirection,
  }: {
    x: number;
    y: number;
    actionDirection: DirectionType;
  }): Array<StaticBlack | undefined> {
    let tX = x / 50;
    let tY = y / 50;
    const bounds: Array<StaticBlack | undefined> = [];
    switch (actionDirection) {
      case "top":
        tY -= 1;
        break;
      case "down":
        tY += 1;
        break;
      case "left":
        tX -= 1;
        break;
      case "right":
        tX += 1;
        break;
      default:
        break;
    }
    bounds.push(this.#valueMap.get(`${Math.floor(tY)}${Math.floor(tX)}`));
    if (!Number.isInteger(tX))
      bounds.push(this.#valueMap.get(`${Math.floor(tY)}${Math.ceil(tX)}`));
    if (!Number.isInteger(tY))
      bounds.push(this.#valueMap.get(`${Math.ceil(tY)}${Math.floor(tX)}`));
    return bounds;
  }
}

export const mainMap = new MainMap();
